package com.spark.lola.controller.sup;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.constant.SupOrderAction;
import com.spark.lola.entity.pur.ReturnOrderInfo;
import com.spark.lola.entity.pur.OrderLocation;
import com.spark.lola.mapper.pur.ReturnOrderInfoMapper;
import com.spark.lola.service.pur.IReturnOrderInfoService;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.mybatis.controller.BaseController;
import com.spark.lola.support.mybatis.entity.PageParam;
import com.spark.lola.support.mybatis.entity.QueryParam;
import com.spark.lola.support.wms.configuration.WmsProperties;
import com.spark.lola.support.wms.request.OrderActionRequest;
import com.spark.lola.support.wms.request.RejectRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dengyejun
 * @since 2020-04-24
 */
@RestController
@RequestMapping("/api/pur/return-order-info")
public class ReturnOrderInfoController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(ReturnOrderInfoController.class);
    @Autowired
    private ReturnOrderInfoMapper currentMapper;
    @Autowired
    private IReturnOrderInfoService currentService;
    @Autowired
    private WmsProperties wmsProperties;


    @RequiresPermissions(value = "m5270")
    @RequestMapping(value = "/findBy", consumes = "application/json")
    public ResponseBean findBy(@RequestBody QueryParam<ReturnOrderInfo> queryBean,
                               @SessionAttribute ShiroUser shiroUser) {
        // 是否接货员
        if(wmsProperties.getPurchase().getDutyId().get(2).equals(shiroUser.getDutyId())) {
            bindSelfQuery(queryBean,"o.consigneeId",shiroUser);
        }

        PageParam<ReturnOrderInfo> pageParam = queryBean.toPageParam();
        IPage<ReturnOrderInfo> page = currentMapper.page(pageParam.getPage(), pageParam.getEntityWrapper());
        return ResponseBean.success(page);
    }
    
    @RequiresPermissions(value = "m5274")
    @RequestMapping(value = "/findInfo/{id}", method = RequestMethod.GET)
    public ResponseBean findInfo(@PathVariable Long id,
                                 @SessionAttribute ShiroUser shiroUser) {
        ReturnOrderInfo model = currentService.findFullById(id);
        return ResponseBean.success(model);
    }

    @RequiresPermissions(value = {"m5271", "m5273"}, logical = Logical.OR)
    @RequestMapping(value = "/save", produces = "application/json;charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean save(@RequestBody ReturnOrderInfo form, @SessionAttribute ShiroUser shiroUser) throws Exception {
        ReturnOrderInfo model = null;
        if (form.getId() != null) {
            // 禁止修改项 做空处理
            model = currentService.update(form,shiroUser);
        }
        return ResponseBean.success(model);
    }

    @RequiresPermissions("m5275")
    @RequestMapping(value = "/checking/{orderId}", produces = "application/json;charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean checking(@PathVariable Long orderId, @SessionAttribute ShiroUser shiroUser) throws Exception {
        currentService.checking(orderId,shiroUser);
        return ResponseBean.success();
    }

    @RequiresPermissions("m5276")
    @RequestMapping(value = "/eceiving/{orderId}", produces = "application/json;charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean eceiving(@PathVariable Long orderId, @SessionAttribute ShiroUser shiroUser) throws Exception {
        currentService.completing(orderId,shiroUser);
        return ResponseBean.success();
    }

    @RequiresPermissions("m5277")
    @RequestMapping(value = "/canceling/{orderId}", produces = "application/json;charset=UTF-8")
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean canceling(@PathVariable Long orderId, @SessionAttribute ShiroUser shiroUser) throws Exception {
        currentService.canceling(orderId,shiroUser);
        return ResponseBean.success();
    }

}
